#include "Hunter.h"
#include <Windows.h>
#include <mmsystem.h>

CHunter::CHunter(void){
	this->position.SetZero();
	this->hp = 100;
	this->speed = 0;
	this->damage = 0;
	this->attackRate = 0;
	this->target = NULL;
}
CHunter::CHunter(float posX, float posY, float posZ, int hp, int speed, int damage, int attackRate)
{
	this->position.x = posX;
	this->position.y = posY;
	this->position.z = posZ;
	this->hp = hp;
	this->speed = speed;
	this->damage = damage;
	this->attackRate = attackRate;
	this->target = NULL;
}
CHunter::~CHunter(void){}

void CHunter::setPosX(float value)
{
	this->position.x = value;
}
void CHunter::setPosY(float value)
{
	this->position.y = value;
}
void CHunter::setPosZ(float value)
{
	this->position.z = value;
}
void CHunter::setHp(int value)
{
	this->hp = value;
}
void CHunter::setDamage(int value)
{
	this->damage = value;
}
void CHunter::setSpeed(int value)
{
	this->speed = value;
}
float CHunter::getPosX(void)
{
	return this->position.x;
}
float CHunter::getPosY(void)
{
	return this->position.y;
}
float CHunter::getPosZ(void)
{
	return this->position.z;
}
int CHunter::getHp(void)
{
	return this->hp;
}
int CHunter::getSpeed(void)
{
	return this->speed;
}
int CHunter::getDamage(void)
{
	return this->damage;
}
Vector3 CHunter::getPos()
{
	return position;
}

void CHunter::setCurrentState(State newState)
{
	this->hunterState = newState;
}
State CHunter::getCurrentState(void)
{
	return this->hunterState;
}

CMonster* CHunter::getTarget(){
	return target;
}

void CHunter::Update(CMonster *monster, float dt)
{
	float distX;
	float distY;
	float distZ;
	float distanceBetween;

	if (monster->isActive()){
		distX = monster->getPosition().x - this->getPosX();
		distY = monster->getPosition().y - this->getPosY();
		distZ = monster->getPosition().z - this->getPosZ();
		distanceBetween = sqrt(distX * distX + distY * distY + distZ * distZ);
	}
	else{
		distX = 100;
		distY = 100;
		distZ = 100;
		distanceBetween = 110;
	}

	switch (this->getCurrentState())
	{
	case State::STATE_IDLE:
	{
		static int lastAtkTime = timeGetTime();
		if (timeGetTime() - lastAtkTime > 50 && this->hp < 50)
		{
			this->hp += 1;
			lastAtkTime = timeGetTime();
		}

		if (this->hp >= 50)
		{
			this->setCurrentState(STATE_HUNT);
		}
		if (this->hp <= 0)
		{
			this->setCurrentState(STATE_DIE);
		}
	}
	break;

	case State::STATE_HUNT:
		srand((unsigned)time(0));
		this->position.Set(this->position.x += Math::RandIntMinMax(-30.0, 30.0) * dt, this->position.y += Math::RandIntMinMax(-30.0, 30.0) * dt, 0);

		if (distanceBetween <= 30)
		{
			this->setCurrentState(STATE_ATTACK);
			this->target = monster;
		}
		if (this->hp <= 0)
		{
			this->setCurrentState(STATE_DIE);
		}
		break;

	case State::STATE_ATTACK:
	{
		static int lastAtkTime = timeGetTime();

		if (distanceBetween <= 40 && distanceBetween >= 10 && monster == target)
		{
			this->position.Set(this->position.x += (distX)* dt, this->position.y += (distY)* dt, 0);
		}
		else
		{
			if (timeGetTime() - lastAtkTime > 1000){
				target->setHP(target->getHP() - (rand() % 25 + 1));
				lastAtkTime = timeGetTime();
			}
		}

		if (!this->target->isActive())
		{
			this->setCurrentState(STATE_IDLE);
		}
		if (this->hp <= 15)
		{
			this->setCurrentState(STATE_FLEE);
		}
		if (this->hp <= 0)
		{
			this->setCurrentState(STATE_DIE);
		}
	}
	break;

	case State::STATE_FLEE:
	{
		Vector3 escape = this->position - target->getPosition();
		escape.Normalize();
		this->position.Set(this->position.x += escape.x, this->position.y += escape.y, 0);

		if (this->hp <= 0)
		{
			this->setCurrentState(STATE_DIE);
		}

		if (target->getState() == MonsterState::RETURN)
		{
			this->setCurrentState(STATE_IDLE);
		}
		break;
	}
	case State::STATE_DIE:
		break;
	}
}

void CHunter::render(void)
{
	glPushMatrix();
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_BLEND);
	glTranslatef(this->getPos().x, this->getPos().y, this->getPos().z);
	glBegin(GL_QUADS);
	glTexCoord2f(0, 1); glVertex2f(0, 64);
	glTexCoord2f(0, 0); glVertex2f(0, 0);
	glTexCoord2f(1, 0); glVertex2f(64, 0);
	glTexCoord2f(1, 1); glVertex2f(64, 64);
	glEnd();
	glDisable(GL_BLEND);
	glDisable(GL_TEXTURE_2D);
	glPopMatrix();
}